package mobile.persistencia;

import waba.io.*;
import waba.util.Vector;
import waba.sys.*;
import mobile.dominio.*;
import moio.util.*;

public class PCliente extends Catalog {
	private ResizeRecord rs;
	private DataStream ds;

	public static PCliente instance = new PCliente();

	/**
	 * Constructor which creates or open ( if it already exists ) the Customers.pdb file,
	 * attach the ResizeRecord and DataStream. The pragmatic way of reading/writing to a
	 * Catalog is by attaching those streams (ResizeRecord/DataStream) so that the resizing
	 * of the Catalog and information writing/reading are performed by those classes
	 */
	public PCliente()
	{
		super("Cliente.mGqv.DATA", Catalog.CREATE);
		//super("Cliente."+ Settings.appCreatorId +".DATA", Catalog.CREATE);
		rs = new ResizeRecord(this, 256);
		ds = new DataStream(rs);
	}

	public void write(Cliente unCliente, boolean update, boolean mantenerid){
		try {
			if (update){
				//int n = indexOf(unCliente);
				//if (n == -1 || !rs.restartRecord(unCliente.getOrden())) 
				//	return;
				rs.restartRecord(unCliente.getOrden() - 1);
			}else{
				rs.startRecord(); /* start the record at the next available position */
				if(!mantenerid){
					Parametros unParametro = PParametros.instance.getParametroXID(1);
					unCliente.setId(unParametro.getUltimoCliente() + 1);
					unCliente.setOrden(unParametro.getMayororden() + 1);
					unParametro.setMayororden(unCliente.getOrden());
					unParametro.setUltimoCliente(unCliente.getId());
					PParametros.instance.write(unParametro, true);
				}
			}

			unCliente.saveTo(ds);
			rs.endRecord(); /* finishes the writing of a record */
			close();
			instance = new PCliente();
		} catch (IOError e) {}
	}

	/**
	 * Removes a record ( Customer ) from the pdb file
	 * 
	 * @param cli
	 *           the Customer
	 */
	public void remove(Cliente unCliente)
	{
		/*
		 * We must set the record position to the record we want to delete, and then delete
		 * the record.
		 */
		int n = indexOf(unCliente);
		try {
			if (n >= 0 && this.setRecordPos(n)){
				this.deleteRecord();
			}
		} catch (IOError e) {}
	}

	/**
	 * Find the index ( position ) of the record ( Customer ) inside the pdb file
	 * 
	 * @param cli
	 *           the Customer
	 * @return the index position or -1 if the Customer was not found on the record
	 */
	public int indexOf(Cliente unCliente)
	{
		for (int n = this.getRecordCount() - 1; n >= 0; n--)
			try {
				if (this.setRecordPos(n)) {
					Cliente elCliente = read();

					if (elCliente.getId() == unCliente.getId()) {
						return n;
					}
				}
			} catch (IOError e) {

			}
			return -1;
	}

	/**
	 * Reads a record ( Customer ) and construct a Customer. Notice that the order in which
	 * you read informations from the Customer is relevant.
	 * 
	 * @return the Customer
	 */
	public Cliente read()
	{
		return new Cliente(ds);
	}

	/**
	 * Returns a specific record ( Customer )
	 * 
	 * @param name
	 *           Name of the Customer
	 * @return The Customer or null if the Customer was not found
	 */
	public Cliente obtenerClienteXID(int id) // info
	{
		for (int n = this.getRecordCount() - 1; n >= 0; n--)
			try {
				if (this.setRecordPos(n)){
					Cliente unCliente = read();
					if (id == unCliente.getId()){
						return unCliente;
					}
				}
			} catch(IOError e){
			}
			return null;
	}
	
	public ArrayList buscarClientes(String nombre){
		ArrayList colClientes = new ArrayList();

		int n = this.getRecordCount();
		for (int i = 0; i < n; i++){
			try {
				if (this.setRecordPos(i)){
					Cliente unCliente = read();

					if(unCliente.getNombre().toUpperCase().indexOf(nombre.toUpperCase()) >= 0){
						colClientes.add(unCliente);
					}
				}
			} catch (IOError e) {}
		}
		return colClientes;
	}
	
	public Cliente buscarSiguienteCliente (int orden){
		Parametros unParametros = PParametros.instance.getParametroXID(1);
		if(unParametros != null){
			if (orden == 0){
				orden = unParametros.getUltimoOrdenCliente();
			}
			if (this.setRecordPos(orden)){
				Cliente unCliente = read();
				return unCliente;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	public Cliente buscarAnteriorCliente (int orden){
		Parametros unParametros = PParametros.instance.getParametroXID(1);
		if(unParametros != null){
			if (orden != 0){
				unParametros.setUltimoOrdenCliente(orden-1);
			}
			if (this.setRecordPos(unParametros.getUltimoOrdenCliente()-1)){
				Cliente unCliente = read();
				return unCliente;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	public Cliente buscarClienteXOrden(int orden){
		if (this.setRecordPos(orden-1)){
			Cliente unCliente = read();
			return unCliente;
		}else{
			return null;
		}
	}
}
